import React, {FC} from 'react';
import {Button, Checkbox, Col, Row} from "antd";
import {EditModalFooterProps} from "@/global";

/**
 * 编辑对话框尾部控件
 */
const EditModalFooter: FC<EditModalFooterProps> = props => {
  const footerArray = [];
  const okButton = props.okVisible ? (
    // @ts-ignore
    <Button {...props.okProps} key="submit" type={props.okType} loading={props.okLoading} onClick={props.onOk}>{props.okText}</Button>
  ) : null;
  const cancelButton = props.cancelVisible ? (
    <Button {...props.cancelProps} key="cancel" onClick={props.onCancel}>{props.cancelText}</Button>
  ) : null;

  if (props.okFirst) {
    if (okButton != null) footerArray.push(okButton);
    if (cancelButton != null) footerArray.push(cancelButton);
  } else {
    if (cancelButton != null) footerArray.push(cancelButton);
    if (okButton != null) footerArray.push(okButton);
  }
  props.buttons && props.buttons.forEach(p => footerArray.push(p));

  return (<Row align="middle">
    <Col span={12} style={{textAlign: 'left'}}>
      {props.continueSaveVisible && (
        <Checkbox {...props.continueSaveProps} checked={props.continueSaveChecked}
                  onChange={props.onContinueSaveChange}>{props.continueSaveText}</Checkbox>
      )}
      {props.resetFormVisible && (
        <Checkbox {...props.resetFormProps} checked={props.resetFormChecked}
                  onChange={props.onResetFormChange}>{props.resetFormText}</Checkbox>
      )}
    </Col>
    <Col span={12} style={{textAlign: 'right'}}>
      {footerArray && footerArray.map(p => (p))}
    </Col>
  </Row>);
};

/** 组件默认值 */
EditModalFooter.defaultProps = {
  okVisible: true,
  okFirst: false,
  okLoading: false,
  okText: '确定',
  okType: 'primary',
  cancelVisible: true,
  cancelText: '取消',
  continueSaveVisible: true,
  continueSaveText: '连续新增',
  continueSaveChecked: false,
  resetFormVisible: false,
  resetFormText: '重置表单',
  resetFormChecked: false,
};

export default React.memo(EditModalFooter);
